
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Java,idea-plugin," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>环境搭建——idea插件开发 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        环境搭建——idea插件开发
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T17:13:31.000Z">
        2020-01-19
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/" rel="tag">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/idea-plugin/" rel="tag">idea-plugin</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 2 分钟</span>
    </header>

    <div class="post-content">
      <h2 id="开发工具"><a href="#开发工具" class="headerlink" title="开发工具"></a>开发工具</h2><p>idea的插件肯定就是用idea。<br>下载社区版或者专业版都可以，链接：<a target="_blank" rel="noopener" href="https://www.jetbrains.com/idea/download/">https://www.jetbrains.com/idea/download/</a>。</p>
<h2 id="编译工具配置"><a href="#编译工具配置" class="headerlink" title="编译工具配置"></a>编译工具配置</h2><h3 id="JDK"><a href="#JDK" class="headerlink" title="JDK"></a>JDK</h3><p>至少1.8或者以上的JDK版本。JDK的下载安装、配置自行处理。</p>
<h3 id="Gradle"><a href="#Gradle" class="headerlink" title="Gradle"></a>Gradle</h3><p>项目编译使用gradle，请自行安装配置gradle。<br>gradle下载地址：<a target="_blank" rel="noopener" href="https://services.gradle.org/distributions/">https://services.gradle.org/distributions/</a></p>
<h3 id="启用idea的-Plugin-Devkit"><a href="#启用idea的-Plugin-Devkit" class="headerlink" title="启用idea的 Plugin Devkit"></a>启用idea的 Plugin Devkit</h3><p>在插件里面确认plugin devkit已经安装成功，如果没有则安装。</p>
<p><img src="/image/idea-plugin/1-7.png" alt="devkit"></p>
<h2 id="创建第一个插件项目"><a href="#创建第一个插件项目" class="headerlink" title="创建第一个插件项目"></a>创建第一个插件项目</h2><p>打开idea，从左上角的File里面创建一个gradle项目。</p>
<blockquote>
<p> File -&gt; New -&gt; Project…</p>
</blockquote>
<p><img src="/image/idea-plugin/1-1.png" alt="选中Java、Intellij Platfrorm Plugin"></p>
<p><strong>Java、Intellij Platform Plugin</strong>这两个是必须的，JDK至少<code>1.8</code>或者以上版本。**Kotlin(Java)**可选。继续下一步。</p>
<p><img src="/image/idea-plugin/1-2.png" alt="group"></p>
<p>输入GroupId，ArtifactId，继续下一步。</p>
<p><img src="/image/idea-plugin/1-3.png" alt="gradle设置"></p>
<p>设置自己安装的gradle。选中<code>Use auto-import</code>，JDK默认即可。继续下一步。</p>
<p><img src="/image/idea-plugin/1-4.png" alt="项目名称"></p>
<p>输入项目名称。其他默认即可。最后点击Finish，等待gradle自动下载项目依赖，如果下载很慢可以引入本地 Intellij SDK。</p>
<p>Intellij SDK下载：<a target="_blank" rel="noopener" href="https://d2cico3c979uwg.cloudfront.net/com/jetbrains/intellij/idea/ideaIC/2019.1.4/ideaIC-2019.1.4.zip">https://d2cico3c979uwg.cloudfront.net/com/jetbrains/intellij/idea/ideaIC/2019.1.4/ideaIC-2019.1.4.zip</a></p>
<p>我下载的是和当前Idea同一个版本的SDK。然后修改<code>build.gradle</code>文件的里面的<code>repositories</code>标签内容，引入本地SDK。</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">repositories &#123;</span><br><span class="line">    maven &#123;</span><br><span class="line">        url <span class="string">&#x27;/Users/ubuntuvim/software/sdk/ideaIC-2019.1.4.zip&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    maven &#123;</span><br><span class="line">        url <span class="string">&#x27;https://dl.bintray.com/jetbrains/intellij-plugin-service&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    maven &#123; url <span class="string">&#x27;https://cache-redirector.jetbrains.com/jetbrains.bintray.com/intellij-plugin-service&#x27;</span> &#125;</span><br><span class="line">    maven &#123; url <span class="string">&#x27;https://cache-redirector.jetbrains.com/repo1.maven.org/maven2&#x27;</span> &#125;</span><br><span class="line">    maven &#123; url <span class="string">&#x27;https://cache-redirector.jetbrains.com/jcenter.bintray.com&#x27;</span> &#125;</span><br><span class="line">    mavenCentral()</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>自定义的路径一定记得放在<code>mavenCentral()</code>的前面，下载的顺序是如果前面的URL已经能获取到SDK则后面的不再下载，由于SDK比较大，通过maven下载会很慢。自己另外下载之后直接引入即可。</p>
<p>等待gradle自动编译完之后可以看到gradle的命令。</p>
<p><img src="/image/idea-plugin/1-5.png" alt="gradle命令"></p>
<p>都配置好之后，创建一个简单的hello world程序验证环境是否搭建成功。</p>
<p>如下所示，创建一个简单的<code>Action</code>类，创建一个Plugin的类和普通类有所差别，在包上<code>右键 -&gt; New -&gt; Plugin Devkit -&gt; Action </code>。</p>
<p><img src="/image/idea-plugin/1-8.png" alt="Devkit"></p>
<p>简单设置一下Action的信息。</p>
<p><img src="/image/idea-plugin/1-9.png" alt="Action Class"></p>
<p>第一个红色框里面是自定义的Action类的名字；</p>
<p>第二个红色框设置的意思是新建的Action菜单放在Idea的<strong>Help</strong>菜单下；</p>
<p>第三个红色框意思是把新增的Action菜单放在<strong>Help</strong>菜单下的一个子菜单。</p>
<p>其他默认即可。</p>
<p>类的内容很简单：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.idea.plugin;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.intellij.openapi.actionSystem.AnAction;</span><br><span class="line"><span class="keyword">import</span> com.intellij.openapi.actionSystem.AnActionEvent;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>： 第一个测试Action</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: ubuntuvim</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@HomePage</span>： http://xcoding.tech</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span>: 2020-01-20 00:16</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppPlugin</span> <span class="keyword">extends</span> <span class="title">AnAction</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">actionPerformed</span><span class="params">(AnActionEvent e)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Hello my plugin...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Action创建好之后，<code>idea-test-plugin2/src/main/resources/META-INF/plugin.xml</code>项目下的这个文件中会自动增加以下配置信息：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Add your actions here --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">action</span> <span class="attr">id</span>=<span class="string">&quot;com.ubuntuvim.idea.plugin&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.ubuntuvim.idea.plugin.AppPlugin&quot;</span> <span class="attr">text</span>=<span class="string">&quot;my-plugin&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">description</span>=<span class="string">&quot;我的第一个plugin&quot;</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">add-to-group</span> <span class="attr">group-id</span>=<span class="string">&quot;HelpMenu&quot;</span> <span class="attr">anchor</span>=<span class="string">&quot;first&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">action</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>最后，我们尝试启动一个自定义的Action，看下会是一个什么样的效果，和期待有没有。</p>
<p>直接运行新建的Action，第一次运行会比较慢，等待gradle自动编译，可以看到以下运行日志。</p>
<p><img src="/image/idea-plugin/1-11.png" alt="run log"></p>
<p>运行完毕之后，会从新打开一个Idea窗口。新打开的窗口里面会包含了你自定义的Action，其实说白了就是自己扩展里IDE的功能。</p>
<p>新打开的Idea创建有可能是一个空的，你也可以直接打开一个本地已经创建过的项目。</p>
<p><img src="/image/idea-plugin/1-12.png" alt="open window"></p>
<p>可以看到在新打开的创建中，在<strong>Help</strong>菜单下多了一个自定义的子菜单，点击这个子菜单，此时不会有任何反应，因为我们并没有实现任何操作，只是打印了一个控制台日志。回到原来开发插件的窗口，可以在控制台上看到输出了<code>System.out</code>日志。</p>
<p><img src="/image/idea-plugin/1-14.png" alt="out log"></p>
<p>虽然很简单，但是起码验证了开发插件的环境是搭建成功了。另外，新打开的创建也是一个正常可以用的Idea窗口，你可以在里面创建、开发其他的项目。与正常的Idea窗口无差别。</p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/hello-world/" rel="next" title="Hello World">
          Hello World
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/21/idea-plugin/%E5%AE%98%E6%96%B9FAQ(%E9%9D%9E%E5%B8%B8%E9%87%8D%E8%A6%81)/" rel="prev" title="官方FAQ(非常重要)——idea插件开发">
            官方FAQ(非常重要)——idea插件开发
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7"><span class="toc-text">开发工具</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E8%AF%91%E5%B7%A5%E5%85%B7%E9%85%8D%E7%BD%AE"><span class="toc-text">编译工具配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JDK"><span class="toc-text">JDK</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Gradle"><span class="toc-text">Gradle</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E7%94%A8idea%E7%9A%84-Plugin-Devkit"><span class="toc-text">启用idea的 Plugin Devkit</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AA%E6%8F%92%E4%BB%B6%E9%A1%B9%E7%9B%AE"><span class="toc-text">创建第一个插件项目</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '环境搭建——idea插件开发', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
